/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
function getQuantity(key){                
    // lay ra cac cap key/value bang cach tach String cookie boi cac dau ;
    var cookies = document.cookie.split(";");
    var index;
    for(index = 0; index < cookies.length; index++)
    {
        cookieEntry = cookies[index].split("=");
        // first part of the split string holds the key ...
        if (key == cookieEntry[0])
        {           
            // ... and the second one the value
            return cookieEntry[1];
        }
    }
    return null;
}
            
function buy(c_name){    
    var c_value=escape(parseInt(getQuantity(c_name)) + 1);
    if(c_value == "NaN"){
        c_value = 1;
    }    
    document.cookie=c_name + "=" + c_value;
    alert("You are having " + getQuantity(c_name) + " of this book in the cart.");
}

function buyMany(){
    var c_name = parseInt(document.getElementById("bookID").innerHTML);
    var element = document.getElementById("quan");
    var quan = element.options[element.selectedIndex].value;
    
    var c_value=escape(parseInt(getQuantity(c_name)) + parseInt(quan));
    if(c_value == "NaN"){
        c_value = parseInt(quan);
    }
    document.cookie=c_name + "=" + c_value;    
    alert("You are having " + getQuantity(c_name) + " of this book in the cart.");
}
            
function deleteCookie(key)
{
    // Delete a cookie by setting the date of expiry to yesterday
    date = new Date();
    date.setDate(date.getDate() -1);
    document.cookie = escape(key) + '=;expires=' + date;
}
            
function deleteAllCookies() {
    var cookies = document.cookie.split(";");
 
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

function back(){
    window.history.back();
}

function remove(){    
    var isSure = confirm("Are you sure?");
    if(isSure){
        deleteAllCookies();
        window.location = "index.jsp"
    } 
}

function check_num(obj,length,e){    
    var key=window.event?e.keyCode:e.which;
    var len=obj.value.length+1;
    if(length<=3)begin=48;else begin=45;
    if(key>=begin&&key<=57&&len<=length||(key==8||key==0)){}
    else return false;
} 

function getElementsByClassName(node,classname) {
    if (node.getElementsByClassName) { // use native implementation if available
        return node.getElementsByClassName(classname);
    } else {
        return (function getElementsByClass(searchClass,node) {
            if ( node == null )
                node = document;
            var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

            for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                    classElements[j] = els[i];
                    j++;
                }
            }
            return classElements;
        })(classname, node);
    }
}

function update(){
    var quantities = document.getElementsByTagName("input");    
    var count = 0;
    for(var i = 0; i < quantities.length; i++){
        var id = quantities[i].id;
        var value = quantities[i].value;        
        updateCookie(id, value);
        count++;
    }    
    
    alert("Update " + count + " rows successful.");
    window.location = "cart.jsp";
}

function updateCookie(id, value){
    var c_name = parseInt(id);            
    var c_value=escape(value);    
    document.cookie=c_name + "=" + c_value;
}

function checkout(){
    window.location = "http://localhost:8080/FinalProject/confirm.jsp";
}
